package com.oauth.extensions.client;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.auth.client.beans.MemberClientBean;
import com.oauth.auth.client.rpc.MemberRemoteService;
import com.oauth.auth.client.rpc.MemberRemoteServiceAsync;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.components.client.rpc.exception.SessionExpiredException;
import com.oauth.components.client.ui.HasWidgetInfo;
import com.oauth.extensions.client.ui.Header;
import com.oauth.extensions.client.ui.LoginPanel;
import com.oauth.facebook.client.ui.FacebookInfo;
import com.oauth.global.client.ui.birthday.BirthdayNotification;
import com.oauth.google.client.ui.orkut.OrkutInfo;
import com.oauth.linkedin.client.ui.LinkedinInfo;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class BirthdayExtensionEntryPoint implements EntryPoint {

	private final MemberRemoteServiceAsync memberService = GWT.create(MemberRemoteService.class);

	private static ExtensionEntryPointUiBinder uiBinder = GWT
			.create(ExtensionEntryPointUiBinder.class);

	interface ExtensionEntryPointUiBinder extends UiBinder<Widget, BirthdayExtensionEntryPoint> {
	}

	Map<Long, HasWidgetInfo> widgetMap = new HashMap<Long, HasWidgetInfo>();

	@UiField
	LoginPanel loginPanel;

	@UiField
	DeckPanel deckPanel;

	@UiField
	BirthdayNotification birthdayNotification;

	@UiField
	Header header;

	@UiField
	Button extensionUrl;

	public void onModuleLoad() {
		Widget p = uiBinder.createAndBindUi(this);
		deckPanel.showWidget(0); // show loading image.
		memberService.getMember(new OAuthAsnyCallBack<MemberClientBean>() {
			public void onSuccess(MemberClientBean result) {
				if (result == null) {
					deckPanel.showWidget(1); // shows sign in button.
				} else {
					deckPanel.showWidget(2);// show birthdays.
					widgetMap.put(2l, (HasWidgetInfo) GWT.create(OrkutInfo.class));
					widgetMap.put(3l, (HasWidgetInfo) GWT.create(LinkedinInfo.class));
					widgetMap.put(4l, (HasWidgetInfo) GWT.create(FacebookInfo.class));
					birthdayNotification.setWidgetMap(widgetMap);

					header.setUserName(result.getEmail());
					birthdayNotification.initBirthdays();
				}
			}

			public void onFailure(Throwable pCaught) {
				try {
					throw pCaught;
				} catch (SessionExpiredException e) {
					deckPanel.showWidget(1); // shows sign in button.
				} catch (Throwable e) {
					deckPanel.showWidget(3);
				}
			}
		});

		RootLayoutPanel rp = RootLayoutPanel.get();
		rp.add(p);
	}

	@UiHandler("extensionUrl")
	public void onClick(ClickEvent e) {
		Window.open(
				"https://chrome.google.com/extensions/detail/hakmidadolofogapnmkcpehegmfhllmp?hl=en",
				"RapWaves.com - Extensions", null);
	}

	public static native void setBadgeText(String count) /*-{
		chrome.browserAction.setBadgeText(count);
	}-*/;
}
